#include "Logic/Item.h"
#include "Logic/ItemContainer.h"
#include "Base.h"

#define checkItemIndxe(index,msg,action) \
	if (index >= m_maxCount || index < 0)\
	{\
		MY_ASSERT(false,msg,action;);\
	}


ItemContainer::ItemContainer( int maxCount )
{
	m_maxCount = maxCount;
	m_items = new Item*[m_maxCount];
	for (int i = 0; i < m_maxCount; ++i)
	{
		m_items[i] = NULL;
	}
}

ItemContainer::~ItemContainer()
{
	for (int i = 0; i < m_maxCount; ++i)
	{
		SAFE_DELETE(m_items[i]);
	}

	SAFE_DELETE_ARRAY(m_items);
}

Item* ItemContainer::getItemByIndex( int index )const
{	
	checkItemIndxe(index,"ItemContainer::getItemByIndex Invalid index",return NULL;);

	return m_items[index];
}

Item* ItemContainer::getItemByUID( ItemID _uid ) const
{
	for (int i = 0; i < m_maxCount; ++i)
	{
		if (m_items[i] && m_items[i]->getUID() == _uid)
		{
			return m_items[i];
		}
	}
	return NULL;
}

int ItemContainer::getFristFreeIndex() const
{
	for (int i = 0 ; i < m_maxCount; ++i)
	{
		if (m_items[i] == NULL)
		{
			return i;
		}
	}
	return -1;
}

ErrorCode ItemContainer::addItem( Item* pItem,int index )
{
	checkItemIndxe(index,"ItemContainer::addItem Invalid index",return Error_Common;);
	if (m_items[index] != NULL)
	{
		return Error_Common;
	}
	m_items[index] = pItem;
	return Error_OK;
}

ErrorCode ItemContainer::removeItem( int index )
{
	checkItemIndxe(index,"ItemContainer::removeItem Invalid index",return Error_Common;);

	SAFE_DELETE(m_items[index]);
	return Error_OK;
}

ErrorCode ItemContainer::swapItem( int indexSrc,int indexDest )
{
	checkItemIndxe(indexSrc,"ItemContainer::swapItem Invalid index",return Error_Common;);
	checkItemIndxe(indexDest,"ItemContainer::swapItem Invalid index",return Error_Common;);
	std::swap(m_items[indexSrc],m_items[indexDest]);

	return Error_OK;
}


